package com.ms.mishitong.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ms.mishitong.VO.ConversationVO;
import com.ms.mishitong.VO.UserVO;
import com.ms.mishitong.dto.Result;
import com.ms.mishitong.entity.Conservation;
import com.ms.mishitong.entity.User;
import com.ms.mishitong.mapper.FriendMapper;
import com.ms.mishitong.mapper.UserMapper;
import com.ms.mishitong.service.IFriendListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class FriendListServiceImpl implements IFriendListService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result getFriendList(Long userId) {
        List<ConversationVO> list = friendMapper.getConversationList(userId);
        List<Long> UserIds = new ArrayList<>();
        for (ConversationVO vo : list) {

            if(!vo.getUserId1().equals(userId)){
                vo.setUserId2(vo.getUserId1());
            }
            vo.setUserId1(userId);
            UserIds.add(vo.getUserId2());
        }
        List<UserVO> users = userMapper.GetUserVO(UserIds);
        for (int i = 0; i < list.size() && i < users.size(); i++) {
            ConversationVO vo = list.get(i);
            UserVO user = users.get(i);

            vo.setOtherNickName(user.getNickName());
            vo.setOtherAvatar(user.getIcon());
        }
        log.info("聊天列表:{}", Result.ok(list));

        return Result.ok(list);
    }

}